package cn.tedu.em.advise.webapi.service.impl;

import cn.tedu.em.advise.service.IAdviseService;
import cn.tedu.em.advise.webapi.mapper.AdviseMapper;
import cn.tedu.em.common.exception.EstateManagementServiceException;
import cn.tedu.em.common.pojo.advise.dto.AdviseAddNewDTO;
import cn.tedu.em.common.pojo.advise.dto.AdviseUpdateDTO;
import cn.tedu.em.common.pojo.advise.entity.Advise;
import cn.tedu.em.common.pojo.advise.vo.AdviseListItemVO;
import cn.tedu.em.common.pojo.advise.vo.AdviseStandardVO;
import cn.tedu.em.common.restful.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
@Slf4j
public class AdviseServiceImpl implements IAdviseService {

    @Autowired
    private AdviseMapper adviseMapper;

    public AdviseServiceImpl() {
        log.info("创建业务类的对象：adviseServiceImpl");
    }

    @Override
    public void addNew(AdviseAddNewDTO adviseAddNewDTO) {
        log.info("开始处理【添加建议】的业务，参数：{}", adviseAddNewDTO);

        Advise advise = new Advise();
        BeanUtils.copyProperties(adviseAddNewDTO,advise);

        log.info("即将执行插入数据，参数：{}", advise);
        int rows = adviseMapper.insert(advise);
        if (rows != 1){
            String message = "添加建议失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.info("开始处理【根据ID删除建议】的业务，参数：{}", id);

        AdviseStandardVO adviseStandardVO = adviseMapper.getStandardById(id);
        if (adviseStandardVO == null){
            String message = "删除建议失败，尝试删除的建议数据不存在！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.NOT_FOUND,message);
        }

        log.debug("即将执行删除，参数：{}", id);
        int rows = adviseMapper.deleteById(id);
        if (rows != 1){
            String message = "删除建议失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.INTERNAL_SERVER_ERROR, message);
        }

    }

    @Override
    public void updateInfoById(Long id, AdviseUpdateDTO adviseUpdateDTO) {
        log.debug("开始处理【修改建议】的业务，ID：{}，新数据：{}", id, adviseUpdateDTO);
        AdviseStandardVO adviseStandardVO= adviseMapper.getStandardById(id);

        if (adviseStandardVO==null){
            String message = "修改建议失败，尝试修改的建议数据不 存在！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.NOT_FOUND,message);
        }

        Advise advise = new Advise();
        BeanUtils.copyProperties(adviseUpdateDTO,advise);
        advise.setId(id);
        log.debug("即将修改建议详情：{}", advise);
        int rows = adviseMapper.update(advise);
        if (rows != 1){
            String message = "修改建议失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
    }

    @Override
    public AdviseStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询建议详情】的业务");
        AdviseStandardVO advise = adviseMapper.getStandardById(id);
        if (advise==null){
            String message = "查询建议详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.NOT_FOUND,message);
        }
        return advise;
    }

    @Override
    public List<AdviseListItemVO> list() {
        log.debug("开始处理【查询建议列表】的业务，无参数");
        List<AdviseListItemVO> list = adviseMapper.list();
        return list;
    }

}
